﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace WebSelfHost.Utility.Network
{
    /// <summary>
    /// FTP工具类
    /// </summary>
    //public class FtpUtil
    //{

    ///// <summary>
    ///// 获得文件的最后更新时间
    ///// </summary>
    ///// <param name="ftpPath"></param>
    ///// <param name="ftpUserId"></param>
    ///// <param name="ftpPassword"></param>
    ///// <param name="relativeFilePath"></param>
    ///// <returns></returns>
    //public static DateTime GetFileLastModified(string ftpPath, string ftpUserId, string ftpPassword, string relativeFilePath)
    //{

    //    try
    //    {
    //        FtpWebRequest request = null;
    //        request = (FtpWebRequest)WebRequest.Create(new Uri(Path.Combine(ftpPath, relativeFilePath).Replace("\\", "/")));
    //        request.UseBinary = true;
    //        request.Credentials = new NetworkCredential(ftpUserId, ftpPassword);
    //        request.Method = WebRequestMethods.Ftp.GetDateTimestamp;
    //        FtpWebResponse response = (FtpWebResponse)request.GetResponse();
    //        DateTime dt = response.LastModified;
    //        response.Close();
    //        response = null;
    //        request = null;
    //        return dt;
    //    }
    //    catch (Exception ex)
    //    {
    //        Console.WriteLine("FtpUtil.GetFileLastModified 获得文件的最后更新时间 错误");
    //        throw ex;
    //    }

    //}

    ///// <summary>
    ///// 列出ftp目录下的所有文件
    ///// </summary>
    ///// <param name="ftpPath"></param>
    ///// <param name="ftpUserId"></param>
    ///// <param name="ftpPassword"></param>
    ///// <returns></returns>
    //public static List<string> GetFile(string ftpPath, string ftpUserId, string ftpPassword)
    //{
    //    List<string> list = new List<string>();
    //    try
    //    {
    //        FtpWebRequest request = null;
    //        request = (FtpWebRequest)WebRequest.Create(new Uri(ftpPath));
    //        request.Credentials = new NetworkCredential(ftpUserId, ftpPassword);
    //        request.Method = WebRequestMethods.Ftp.ListDirectory;
    //        FtpWebResponse response = (FtpWebResponse)request.GetResponse();
    //        Stream responseStream = response.GetResponseStream();
    //        MemoryStream stream = new MemoryStream();
    //        byte[] bArr = new byte[1024 * 1024];
    //        int size = responseStream.Read(bArr, 0, (int)bArr.Length);
    //        while (size > 0)
    //        {
    //            stream.Write(bArr, 0, size);
    //            size = responseStream.Read(bArr, 0, (int)bArr.Length);
    //        }
    //        stream.Seek(0, SeekOrigin.Begin);
    //        responseStream.Close();

    //        string str = Encoding.UTF8.GetString(stream.ToArray());
    //        foreach (string line in str.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries))
    //        {
    //            list.Add(line);
    //        }
    //        return list;
    //    }
    //    catch (Exception ex)
    //    {
    //        Console.WriteLine("FtpUtil.GetFile  列出ftp目录下的所有文件 错误");
    //        throw ex;
    //    }

    //}


    ///// <summary>
    ///// 判断是否是文件
    ///// </summary>
    ///// <param name="ftpPath"></param>
    ///// <param name="ftpUserId"></param>
    ///// <param name="ftpPassword"></param>
    ///// <param name="fileName"></param>
    ///// <returns></returns>
    //public static bool IsFile(string ftpPath, string ftpUserId, string ftpPassword, string fileName)
    //{
    //    try
    //    {
    //        FtpWebRequest request = null;
    //        request = (FtpWebRequest)WebRequest.Create(new Uri(ftpPath));
    //        request.Credentials = new NetworkCredential(ftpUserId, ftpPassword);
    //        request.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
    //        FtpWebResponse response = (FtpWebResponse)request.GetResponse();
    //        Stream responseStream = response.GetResponseStream();
    //        MemoryStream stream = new MemoryStream();
    //        byte[] bArr = new byte[1024 * 1024];
    //        int size = responseStream.Read(bArr, 0, (int)bArr.Length);
    //        while (size > 0)
    //        {
    //            stream.Write(bArr, 0, size);
    //            size = responseStream.Read(bArr, 0, (int)bArr.Length);
    //        }
    //        stream.Seek(0, SeekOrigin.Begin);
    //        responseStream.Close();

    //        string str = Encoding.UTF8.GetString(stream.ToArray());
    //        foreach (string line in str.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries))
    //        {
    //            if (!line.ToUpper().Contains("<DIR>"))
    //            {
    //                if (line.Contains(fileName))
    //                {
    //                    return true;
    //                }
    //            }
    //        }
    //        return false;
    //    }
    //    catch (Exception ex)
    //    {
    //        Console.WriteLine("FtpUtil.IsFile  判断是否是文件 错误");
    //        throw ex;
    //    }
    //}


    //#region 文件夹是否存在
    ///// <summary>
    ///// 文件夹是否存在
    ///// </summary>
    ///// <param name="ftpPath">ftp路径</param>
    ///// <param name="ftpUserId">用户名</param>
    ///// <param name="ftpPassword">密码</param>
    ///// <param name="folderName">文件夹名称</param>
    //public static bool FolderExists(string ftpPath, string ftpUserId, string ftpPassword, string folderName)
    //{
    //    try
    //    {
    //        FtpWebRequest request = null;

    //        request = (FtpWebRequest)WebRequest.Create(new Uri(ftpPath));
    //        request.Credentials = new NetworkCredential(ftpUserId, ftpPassword);
    //        request.Method = "LIST";
    //        FtpWebResponse response = (FtpWebResponse)request.GetResponse();
    //        Stream responseStream = response.GetResponseStream();
    //        MemoryStream stream = new MemoryStream();
    //        byte[] bArr = new byte[1024 * 1024];
    //        int size = responseStream.Read(bArr, 0, (int)bArr.Length);
    //        while (size > 0)
    //        {
    //            stream.Write(bArr, 0, size);
    //            size = responseStream.Read(bArr, 0, (int)bArr.Length);
    //        }
    //        stream.Seek(0, SeekOrigin.Begin);
    //        responseStream.Close();

    //        string str = Encoding.UTF8.GetString(stream.ToArray());
    //        foreach (string line in str.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries))
    //        {
    //            if (line.ToUpper().Contains("DIR") || line.ToUpper().Contains("DR"))
    //            {
    //                int pos = line.LastIndexOf(" ");
    //                string folder = line.Substring(pos).Trim();
    //                if (folder == folderName)
    //                {
    //                    return true;
    //                }
    //            }
    //        }
    //    }
    //    catch (Exception ex)
    //    {
    //        Console.WriteLine("FtpUtil.FolderExists 判断文件夹是否存在 错误");
    //        throw ex;
    //    }

    //    return false;
    //}
    //#endregion

    //#region 创建文件夹
    ///// <summary>
    ///// 创建文件夹
    ///// </summary>
    ///// <param name="ftpPath">ftp路径</param>
    ///// <param name="ftpUserId">用户名</param>
    ///// <param name="ftpPassword">密码</param>
    ///// <param name="folderName">文件夹名称</param>
    //public static bool CreateFolder(string ftpPath, string ftpUserId, string ftpPassword, string folderName)
    //{
    //    try
    //    {
    //        FtpWebRequest request = null;

    //        request = (FtpWebRequest)WebRequest.Create(new Uri(Path.Combine(ftpPath, folderName).Replace("\\", "/")));
    //        request.Credentials = new NetworkCredential(ftpUserId, ftpPassword);
    //        request.Method = "MKD";
    //        ((FtpWebResponse)request.GetResponse()).Close();
    //        return true;
    //    }
    //    catch (Exception ex)
    //    {
    //        return false;
    //    }
    //}
    //#endregion

    //#region 文件是否存在
    ///// <summary>
    ///// 文件是否存在
    ///// </summary>
    ///// <param name="ftpPath">ftp路径</param>
    ///// <param name="ftpUserId">用户名</param>
    ///// <param name="ftpPassword">密码</param>
    ///// <param name="relativeFilePath">文件相对路径</param>
    //public static bool FileExists(string ftpPath, string ftpUserId, string ftpPassword, string relativeFilePath)
    //{
    //    try
    //    {
    //        FtpWebRequest request = null;

    //        string folderName = Path.GetDirectoryName(relativeFilePath);
    //        string fileName = Path.GetFileName(relativeFilePath);
    //        request = (FtpWebRequest)WebRequest.Create(new Uri(Path.Combine(ftpPath, folderName).Replace("\\", "/")));
    //        request.Credentials = new NetworkCredential(ftpUserId, ftpPassword);
    //        request.Method = "LIST";
    //        FtpWebResponse response = (FtpWebResponse)request.GetResponse();
    //        Stream responseStream = response.GetResponseStream();
    //        MemoryStream stream = new MemoryStream();
    //        byte[] bArr = new byte[1024 * 1024];
    //        int size = responseStream.Read(bArr, 0, (int)bArr.Length);
    //        while (size > 0)
    //        {
    //            stream.Write(bArr, 0, size);
    //            size = responseStream.Read(bArr, 0, (int)bArr.Length);
    //        }
    //        stream.Seek(0, SeekOrigin.Begin);
    //        responseStream.Close();

    //        string str = Encoding.UTF8.GetString(stream.ToArray());
    //        foreach (string line in str.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries))
    //        {
    //            if (!line.ToUpper().Contains("DIR") && !line.ToUpper().Contains("DR"))
    //            {
    //                int pos = line.LastIndexOf(" ");
    //                string strFileName = line.Substring(pos).Trim();
    //                if (strFileName == fileName)
    //                {
    //                    return true;
    //                }
    //            }
    //        }
    //    }
    //    catch (Exception ex)
    //    {
    //        Console.WriteLine("FtpUtil.FileExists 判断文件是否存在 错误");
    //        throw ex;
    //    }

    //    return false;
    //}
    //#endregion

    //#region 下载文件
    ///// <summary>
    ///// 下载文件
    ///// </summary>
    ///// <param name="ftpPath">ftp路径</param>
    ///// <param name="ftpUserId">用户名</param>
    ///// <param name="ftpPassword">密码</param>
    ///// <param name="relativeFilePath">文件相对路径</param>
    //public static MemoryStream DownloadFile(string ftpPath, string ftpUserId, string ftpPassword, string relativeFilePath)
    //{
    //    try
    //    {
    //        FtpWebRequest request = null;

    //        request = (FtpWebRequest)WebRequest.Create(new Uri(Path.Combine(ftpPath, relativeFilePath).Replace("\\", "/")));
    //        request.Credentials = new NetworkCredential(ftpUserId, ftpPassword);
    //        request.Method = "RETR";
    //        FtpWebResponse response = (FtpWebResponse)request.GetResponse();
    //        Stream responseStream = response.GetResponseStream();
    //        MemoryStream stream = new MemoryStream();
    //        byte[] bArr = new byte[1024 * 1024];
    //        int size = responseStream.Read(bArr, 0, (int)bArr.Length);
    //        while (size > 0)
    //        {
    //            stream.Write(bArr, 0, size);
    //            size = responseStream.Read(bArr, 0, (int)bArr.Length);
    //        }
    //        stream.Seek(0, SeekOrigin.Begin);
    //        responseStream.Close();


    //        return stream;
    //    }
    //    catch (Exception ex)
    //    {
    //        Console.WriteLine("FtpUtil.DownloadFile 下载文件 错误");
    //        throw ex;
    //    }
    //}
    //#endregion

    //#region 异步下载文件
    ///// <summary>
    ///// 异步下载文件
    ///// </summary>
    ///// <param name="ftpPath">ftp路径</param>
    ///// <param name="ftpUserId">用户名</param>
    ///// <param name="ftpPassword">密码</param>
    ///// <param name="relativeFilePath">文件相对路径</param>
    //public static async Task<MemoryStream> DownloadFileAsync(string ftpPath, string ftpUserId, string ftpPassword, string relativeFilePath)
    //{
    //    try
    //    {
    //        FtpWebRequest request = null;


    //        request = (FtpWebRequest)WebRequest.Create(new Uri(Path.Combine(ftpPath, relativeFilePath).Replace("\\", "/")));
    //        request.Credentials = new NetworkCredential(ftpUserId, ftpPassword);
    //        request.Method = "RETR";
    //        FtpWebResponse response = (FtpWebResponse)(await request.GetResponseAsync());
    //        Stream responseStream = response.GetResponseStream();
    //        MemoryStream stream = new MemoryStream();
    //        byte[] bArr = new byte[1024 * 1024];
    //        int size = await responseStream.ReadAsync(bArr, 0, (int)bArr.Length);
    //        while (size > 0)
    //        {
    //            stream.Write(bArr, 0, size);
    //            size = await responseStream.ReadAsync(bArr, 0, (int)bArr.Length);
    //        }
    //        stream.Seek(0, SeekOrigin.Begin);
    //        responseStream.Close();

    //        return stream;
    //    }
    //    catch (Exception ex)
    //    {
    //        Console.WriteLine("FtpUtil.DownloadFileAsync 异步下载文件 错误");
    //        throw ex;
    //    }
    //}
    //#endregion

    //#region 上传文件
    ///// <summary>
    ///// 上传文件
    ///// </summary>
    ///// <param name="ftpPath">ftp路径</param>
    ///// <param name="ftpUserId">用户名</param>
    ///// <param name="ftpPassword">密码</param>
    ///// <param name="relativeFilePath">文件相对路径</param>
    ///// <param name="bArr">文件内容</param>
    //public static bool UploadFile(string ftpPath, string ftpUserId, string ftpPassword, string relativeFilePath, byte[] bArr)
    //{
    //    try
    //    {
    //        FtpWebRequest request = null;
    //        request = (FtpWebRequest)WebRequest.Create(new Uri(Path.Combine(ftpPath, relativeFilePath).Replace("\\", "/")));
    //        request.Credentials = new NetworkCredential(ftpUserId, ftpPassword);
    //        request.Method = "STOR";
    //        Stream postStream = request.GetRequestStream();
    //        int pageSize = 1024 * 1024;
    //        int index = 0;
    //        while (index < bArr.Length)
    //        {
    //            if (bArr.Length - index > pageSize)
    //            {
    //                postStream.Write(bArr, index, pageSize);
    //                index += pageSize;
    //            }
    //            else
    //            {
    //                postStream.Write(bArr, index, bArr.Length - index);
    //                index = index + (bArr.Length - index);
    //            }
    //        }
    //        postStream.Close();
    //        FtpWebResponse response = (FtpWebResponse)request.GetResponse();
    //        Stream responseStream = response.GetResponseStream();
    //        StreamReader sr = new StreamReader(responseStream, Encoding.UTF8);
    //        string result = sr.ReadToEnd();
    //        responseStream.Close();
    //        return true;
    //    }
    //    catch (Exception ex)
    //    {
    //        return false;
    //    }
    //}
    //#endregion

    //#region 删除文件
    ///// <summary>
    ///// 删除文件
    ///// </summary>
    ///// <param name="ftpPath">ftp路径</param>
    ///// <param name="ftpUserId">用户名</param>
    ///// <param name="ftpPassword">密码</param>
    ///// <param name="relativeFilePath">文件相对路径</param>
    //public static bool DeleteFile(string ftpPath, string ftpUserId, string ftpPassword, string relativeFilePath)
    //{
    //    try
    //    {
    //        FtpWebRequest request = null;
    //        request = (FtpWebRequest)WebRequest.Create(new Uri(Path.Combine(ftpPath, relativeFilePath).Replace("\\", "/")));
    //        request.Credentials = new NetworkCredential(ftpUserId, ftpPassword);
    //        request.Method = "DELE";
    //        ((FtpWebResponse)request.GetResponse()).Close();
    //        return true;
    //    }
    //    catch (Exception ex)
    //    {
    //        return false;
    //    }
    //}
    //#endregion
    //}
}
